package component.headerNavigation.sectionMenu
{
	import component.headerNavigation.data.MenuItemData;
	import component.headerNavigation.event.PathEvent;
	import component.headerNavigation.item.SectionMenuItem;
	import component.headerNavigation.layout.MenuLayout;
	
	import flash.events.MouseEvent;
	
	import obecto.core.FLContainer;
	import obecto.layout.ILayout;
	import obecto.transition.State;
	import obecto.transition.Transition;
	import obecto.transition.TransitionsManager;
	
	[Exclude(name="waitChilderCreationComplete", kind="property")]
	[Exclude(name="layout", kind="property")]
	[Exclude(name="contentChildren", kind="property")]
	
	[Event(name="pathChange", type="component.headerNavigation.event.PathEvent")]
	public class SectionMenuClass extends FLContainer
	{	
		protected var _itemsData : Array;
		public function set itemsData(value : Array) : void
		{
			if (value != null && value != _itemsData)
			{
				_itemsData = value;
				if (initialized)
				{
					initializeMenuItems();
				}
			}
		}
		
		override public function set waitChilderCreationComplete(value : Boolean) : void
		{
		}
		override public function set layout(value : ILayout) : void
		{
		}
		override public function set contentChildren(value : Array) : void
		{
		}
		
		[Bindable]
		[Inspectable(category="General", enumeration="vertical,horizontal", defaultValue="vertical")]
		public function set direction(value : String) : void
		{
			MenuLayout(layout).direction = value;
		}
		public function get direction() : String
		{
			return MenuLayout(layout).direction;
		}

		[Bindable]
		[Inspectable(category="General", defaultValue="0")]		
		public function set menuItemSpacing(value : Number) : void
		{
			MenuLayout(layout).itemSpacing = value;
		}
		public function get menuItemSpacing() : Number
		{
			return MenuLayout(layout).itemSpacing;
		}
		
		protected var _selectedPath : String = "";
		[Bindable]
		public function set selectedPath(value : String) : void
		{
			setSelectedPath(value, false);
		}		
		public function get selectedPath() : String
		{
			return _selectedPath;
		}
		
		protected function setSelectedPath(value : String, dispatchEvent : Boolean) : void
		{
			var oldPath : String = _selectedPath;
			var newPath : String = value != null ? value : "";
			
			if (oldPath != newPath)
			{
				deselectAllMenuItems();
				selectMenuItem(newPath);
				
				_selectedPath = newPath;
				
				if(dispatchEvent)
				{
					dispatchSelectedPathChangeEvent(oldPath, newPath);
				}
			}
		}
		
		protected var menuItems : Array;
		
		protected var transitionsManager : TransitionsManager;
		
		[Bindable] protected var collapsedState : State;
		[Bindable] protected var expandedState : State;
		
		public function SectionMenuClass()
		{
			super();
			
			_contentChildren = new Array();
			super.layout = new MenuLayout();
		}
		
		public function show() : void 
		{
			transitionsManager.state = expandedState;
		}
		
		public function hide() : void 
		{
			transitionsManager.state = collapsedState;
		}
		
		override protected function initialize() : void
		{
			super.initialize();
			if (_itemsData != null)
			{
				initializeMenuItems();
			}
		}
		
		protected function initializeMenuItems() : void 
		{
			menuItems = new Array();
			
			for each (var menuItemData : MenuItemData in _itemsData)
			{
				var menuItem : SectionMenuItem = new SectionMenuItem();
				menuItem.data = menuItemData;
				menuItems.push(menuItem);
				
				addContentChild(menuItem);
				
				menuItem.addEventListener(MouseEvent.CLICK, handleItemClick);
			}
			
			layout.items = menuItems;
		}
		
		protected function selectMenuItem(path : String) : void 
		{	
			for each (var menuItem : SectionMenuItem in menuItems)
			{
				if (menuItem.data.id == path)
				{
					menuItem.selected = true;
				}
			}
		}
		
		protected function deselectAllMenuItems() : void 
		{
			for each (var menuItem : SectionMenuItem in menuItems)
			{
				menuItem.selected = false;
			}
		}
		
		protected function dispatchSelectedPathChangeEvent(oldPath : String, newPath : String) : void 
		{
			dispatchEvent(new PathEvent(PathEvent.PATH_CHANGE, oldPath, newPath, false));
		}
		
		private function handleItemClick(e : MouseEvent) : void 
		{
			var menuItem : SectionMenuItem = SectionMenuItem(e.target);
			if (menuItem.enabled)
			{
				if (menuItem.data.id != selectedPath)
				{
					setSelectedPath(menuItem.data.id , true);
				}
				else
				{
					setSelectedPath("", true);
				}
			}
		}
	}
}